<template>
  <div class="login">
    <h1>电商管理系统</h1>
    <div class="login-form-container">
      <div class="logo">
        <img alt="Vue logo" src="../assets/images/ling137.png" />
        <!-- https://cdn.jsdelivr.net/gh/Ling137/imgbed@main/data/137.png -->
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleFormRef"
        label-width="100px"
        class="ruleForm-class"
      >
        <!-- <el-form-item prop="username"> -->
        <el-input v-model="ruleForm.username" placeholder="请输入用户名"
          ><i slot="prefix" class="el-input__icon el-icon-s-custom"></i
        ></el-input>
        <!-- </el-form-item> -->
        <!-- <el-form-item prop="password"> -->
        <el-input
          type="password"
          v-model="ruleForm.password"
          placeholder="请输入密码"
          ><i slot="prefix" class="el-input__icon el-icon-lock"></i
        ></el-input>
        <!-- </el-form-item> -->
        <div class="btn-group">
          <el-button type="primary" size="small" @click="login">登录</el-button>
          <el-button size="small" type="info" @click="resetLoginForm"
            >重置</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'login',
  components: {},
  data() {
    return {
      // 登录表单的数据绑定对象
      ruleForm: {
        username: 'admin',
        password: '123456'
      },
      //   表单的数据验证规则对象,由于没有使用el-form-item包裹input导致验证功能暂时无法使用，后期更改
      rules: {
        //   验证用户名是否合法
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        //   验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 18,
            message: '长度在 6 到 18 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted() {},
  methods: {
    login() {
      // 表单预验证，通过rules规则进行预验证，这里可能由于el-form-item没有使用导致element的框架验证方法没有作用
      this.$refs.ruleFormRef.validate(async valid => {
        if (valid) {
          // const {data:res}解构取值重新赋值
          const { data: res } = await this.$http.post('login', this.ruleForm)
          console.log(res)
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
          this.$message.success(res.meta.msg)
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
        }
      })
    },
    //   重置表单
    resetLoginForm() {
      console.log(this)
      //   this.$refs.ruleFormRef.resetFields()//可能由于缺少el-form-item
      this.ruleForm.username = 'admin'
      this.ruleForm.password = '123456'
    }
  }
}
</script>
<style lang="less" scoped>
/* @import url(); 引入公共css类 */
.login {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(to bottom, #7eb5c6, 50%, #285388);
}
h1 {
  position: absolute;
  top: 80px;
  text-align: center;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: 0.3em;
  font-family: 'Microsoft Yahei', Arial, Helvetica, sans-serif;
  color: #fff;
}
.login-form-container {
  position: relative;
  width: 350px;
  height: 250px;
  border-radius: 8px;
  background-color: #aad0e3;
  display: flex;
  justify-content: center;
  align-items: center;
  .logo {
    position: absolute;
    top: -40px;
    width: 80px;
    height: 80px;
    img {
      border-radius: 50%;
      width: 100%;
      height: 100%;
    }
  }
}

.ruleForm-class {
  /* border: 1px solid red; */
  width: 300px;
  height: 150px;
  text-align: center;
}
.el-input {
  width: 280px;
  margin-top: 10px;
}
// .el-input >>> input {
//   padding-left: 35px;
// }
.btn-group {
  text-align: right;
  padding-right: 10px;
  //   display: flex;
  //   justify-content: flex-end;
}
.el-button {
  margin-top: 25px;
}

.el-form-item {
  margin-bottom: 20px;
}
.el-form-item:nth-child(1) {
  margin-top: 20px;
}
.login-button {
  text-align: center;
}
</style>
